﻿using AdminSytem.EnitiyFrameWorkCore;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

namespace AdminSytem.Repositories
{
    public class Repository<T> : IRepository<T> where T : class
    {
        private readonly AdminStystemDBContext context;

        public Repository(AdminStystemDBContext context)
        {
            this.context = context;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响行数</returns>
        public virtual async Task<int> InsertAsync(T entity)
        {
            context.Set<T>().Add(entity);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 根据实体集合批删
        /// </summary>
        /// <param name="entites"></param>
        /// <returns></returns>
        public virtual async Task<int> DeleteManyAsync(List<T> entites)
        {
            context.Set<T>().RemoveRange(entites);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 根据条件查询第一条
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public  async Task<T> QueryFirstAsync(Expression<Func<T, bool>> predicate)
        {
            return await context.Set<T>().FirstOrDefaultAsync(predicate);

        }
        /// <summary>
        /// 根据条件查询多条
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync(Expression<Func<T, bool>> predicate)
        {
            return await context.Set<T>().Where(predicate).ToListAsync();

        }
        /// <summary>
        /// 查询多条
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> QueryAsync()
        {
            return await context.Set<T>().ToListAsync();

        }
        /// <summary>
        /// 根据条件是否存在
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual async Task<bool> ExitstAsync(Expression<Func<T, bool>> predicate)
        {
            return await context.Set<T>().AnyAsync();

        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task<int> UpdateAsync(T entity)
        {
            context.Set<T>().Update(entity);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="entites"></param>
        /// <returns></returns>
        public virtual async Task<int> UpdateManyAsync(List<T> entites)
        {
            context.Set<T>().UpdateRange(entites);
            return await context.SaveChangesAsync();
        }
        public IQueryable<T> GetAll()
        {
            return context.Set<T>().AsQueryable();
        }
    }
}
